package com.example.activemq.com.example.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @ Author     ：scw
 * @ Date       ：Created in 上午 11:30 2019/7/29 0014
 * @ Description：订阅者消息消费者
 * @ Modified By：
 * @Version: $version$
 */
public class TopicConsumer_Persist {//定义ActivMQ的连接地址
    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    //定义发送消息的队列名称
    private static final String TOPIC_NAME = "topic_persist";
    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("刘备：持久化TOPIC*******消费者的方式");
        //创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //创建连接
        Connection connection = activeMQConnectionFactory.createConnection();
        //打开连接
        connection.setClientID("刘备");
        //connection.start();
        //创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建队列目标
        Topic topic=session.createTopic(TOPIC_NAME);
        //创建持久化订阅
        TopicSubscriber topicSubscriber=session.createDurableSubscriber(topic,"remark.....");
        connection.start();
        Message message = topicSubscriber.receive();
        while (null!=message){
            TextMessage textMessage= (TextMessage) message;
            System.out.println("********收到的持久化topic"+textMessage.getText());
            message=topicSubscriber.receive();
        }
        //保证控制台一直在运行着
        //System.in.read();//监听消费者
        session.close();
        connection.close();

    }
}